﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using WFFSClients;
using System.Collections;

namespace WFFS
{
    public partial class FrmMsg : Form
    {
        public string msg = "";
        public string picmsg = "";
        public FrmAll frmAll = null;
        public FrmMsg()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();
            string x = this.Location.X.ToString();
            string y = this.Location.Y.ToString();
            WriteIniData("FrmMsg", "X", x, Application.StartupPath + @"\WFFS.ini");
            WriteIniData("FrmMsg", "Y", y, Application.StartupPath + @"\WFFS.ini");
        }

        private void FrmMsg_Paint(object sender, PaintEventArgs e)
        {
            GetPicInfo();
            if (msg == "" && picmsg == "")
            {
                return;
            }
            if (msg.IndexOf('\0') >0)
                msg = msg.Substring(0, msg.Length - 1);
            if (picmsg.IndexOf('\0') > 0)
                picmsg = picmsg.Substring(0, picmsg.Length - 1);

            if (msg != "")
            {
                if (richTBInfoDetail.Text.Length == 0)
                {

                    richTBInfoDetail.Text = msg;// +Environment.NewLine;
                }
                else
                {
                    string ms = richTBInfoDetail.Text;
                    string ab = msg + Environment.NewLine + ms; //.Substring(0, msg.Length - 1)
                    richTBInfoDetail.Text = ab;
                }
                msg = "";
            }

            if (picmsg == "")
            {
                return;
            }

            string[] sum = picmsg.ToString().Split(';'); //对图标指令做循环
            string picmsgtemp = "";
            for (int i = 0; i < sum.Length; i++)
            {
                picmsgtemp = sum[i];

                Panel pan = new Panel();
                pan.Dock = DockStyle.Top;
                pan.Size = new System.Drawing.Size(180, 45);
                pan.Location = new Point(0, 0);
                this.panelPicOrder.Controls.Add(pan);

                PictureBox pb = new PictureBox();
                string[] attr = picmsgtemp.ToString().Split('：');
                PictureBox pb1 = new PictureBox();
                foreach (Control c in groupBoxPic.Controls)
                {
                    if (c.Name == attr[1])
                        pb1 = (PictureBox)c;
                }
                pb.BackgroundImage = pb1.BackgroundImage;

                pb.Size = new System.Drawing.Size(40, 40);
                pb.Location = new Point(0, 0);
                pb.Name = "pic";
                pan.Controls.Add(pb);

                Label lb = new Label();
                lb.Text = attr[0];
                lb.Location = new Point(45, 20);
                lb.Size = new Size(120, 20);
                pan.Controls.Add(lb);
                picmsg = "";
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            richTBInfoDetail.Text = "";
            panelPicOrder.Controls.Clear();
        }

        private void FrmMsg_FormClosed(object sender, FormClosedEventArgs e)
        {
            string x = this.Location.X.ToString();
            string y = this.Location.Y.ToString();
            WriteIniData("FrmMsg", "X", x, Application.StartupPath + @"\WFFS.ini");
            WriteIniData("FrmMsg", "Y", y, Application.StartupPath + @"\WFFS.ini");
        }
        public static bool WriteIniData(string Section, string Key, string Value, string iniFilePath)
        {
            if (File.Exists(iniFilePath))
            {
                long OpStation = WritePrivateProfileString(Section, Key, Value, iniFilePath);
                if (OpStation == 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                return false;
            }
        }
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);


        public void GetPicInfo()
        {
            this.panelJC.Controls.Clear();
            DirectoryInfo dir = new DirectoryInfo(Application.StartupPath + "\\alldirective\\");
            int i = 0;
            int j = 0;
            int k = 0;
            FileInfo[] fis = dir.GetFiles();
            for (int l = fis.Length - 1; l >= 0; l--)
            {
                PictureBox pb = new PictureBox();
                Image img = new Bitmap(Application.StartupPath + "\\alldirective\\" + fis[l].Name);
                pb.Image = img;
                pb.SizeMode = PictureBoxSizeMode.StretchImage;
                pb.Name = fis[l].Name;
                pb.Size = new System.Drawing.Size(90, 72);
                pb.TabIndex = i;
                pb.TabStop = false;
                pb.Click += this.pic_Click;

                if (i == 0)
                {
                    pb.Location = new Point(2, 6 + k * 90);
                    k++;
                    this.panelJC.Controls.Add(pb);
                }
                else
                {
                    pb.Location = new Point(2 + i * 90, 6);
                    this.panelJC.Controls.Add(pb);
                    j++;
                }

                Console.WriteLine(pb.Location.Y.ToString());
                i++;
            }
        }

        //概览图点击事件
        private void pic_Click(object sender, EventArgs e)
        {
            Control control = (Control)sender;
            
            if (frmAll == null)
            {
                frmAll = new FrmAll(control.Name);
                string x = ReadIniData("FrmAll", "X", "X", Application.StartupPath + @"\WFFS.ini");
                string y = ReadIniData("FrmAll", "Y", "Y", Application.StartupPath + @"\WFFS.ini");
                frmAll.Location = new Point(int.Parse(x), int.Parse(y));
                frmAll.TopMost = true;
            }
            else if (frmAll.Text == "")
            {
                frmAll.Dispose();
                frmAll = null;
                frmAll = new FrmAll(control.Name);
                string x = ReadIniData("FrmAll", "X", "X", Application.StartupPath + @"\WFFS.ini");
                string y = ReadIniData("FrmAll", "Y", "Y", Application.StartupPath + @"\WFFS.ini");
                frmAll.Location = new Point(int.Parse(x), int.Parse(y));
                frmAll.TopMost = true;
            }
            Image img = new Bitmap(Application.StartupPath + "\\alldirective\\" + control.Name);
            frmAll.Drawing.Visible = true;
            if (frmAll.Drawing.Image != null)
            {
                frmAll.Drawing.Image.Dispose();
            }
            frmAll.Drawing.Image = img;

            frmAll.Show();
            
            
        }

        [DllImport("kernel32")]
        private static extern long GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
        public static string ReadIniData(string Section, string Key, string NoText, string iniFilePath)
        {
            if (File.Exists(iniFilePath))
            {
                StringBuilder temp = new StringBuilder(1024);
                GetPrivateProfileString(Section, Key, NoText, temp, 1024, iniFilePath);
                return temp.ToString();
            }
            else
            {
                return String.Empty;
            }
        }
    }
}
